package com.imooc.service.impl;

import com.imooc.mapper.CategoryCustomMapper;
import com.imooc.mapper.CategoryMapper;
import com.imooc.pojo.Category;
import com.imooc.service.CategoryService;
import com.imooc.vo.NewItemVo;
import com.imooc.vo.RootCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wangke
 * @Description 查询商品的分类实现
 * @Date 2021/7/12 12:43
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryCustomMapper categoryCustomMapper;

    //查询所有的一级菜单分类
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Category> getCategoryParentLevel() {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);  //查询所有一级父菜单
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<RootCategoryVo> queryRootCategory(Integer subId) {
        List<RootCategoryVo> rootCategoryVos = categoryCustomMapper.querySubCategory(subId);

        return rootCategoryVos;
    }
  //首页商品数据加载
    @Override
    @Transactional(propagation =  Propagation.SUPPORTS)
    public List<NewItemVo> getItemsLazy(Integer catId) {
        //构建参数map
        Map catMap = new HashMap<String,Object>();
        catMap.put("catId",catId);
        List<NewItemVo> itemLazy = categoryCustomMapper.getItemLazy(catMap);
        return itemLazy;
    }
}
